# -*-coding:utf-8 -*-

"""
共通日志记录
"""

import traceback

import mqhandler
import timetool


def save_error_log(msg_type='mobile_api_metal_3.2', error_level=1,
                   error_code=1003, interface='', memo='', server_ip='', project_path=''):
    """
    记录项目中的错误发送到消息队列
    :param msg_type: 用来区分哪个app的哪个版本的错误信息 如 mobile_api_metal_3.2|mobile_service_usercenter_1.0
    :param error_level: 错误等级
    :param error_code: 错误码
    :param interface: 访问的接口(MTD提供服务会用到)
    :param memo:主要是对调用的外来接口用到此参数
    :param server_ip:记录项目所在服务器的IP(对应setting.SERVER_IP)
    :param project_path:主要项目的路径(对应setting.PROJECT_PATH)
    :return:
    """
    error_msg = dict()
    error_msg['logtype'] = 16
    error_msg['msg'] = traceback.format_exc()
    error_msg['timestamp'] = timetool.get_timestamp()
    error_msg['type'] = msg_type
    error_msg['server_ip'] = server_ip
    error_msg['project_path'] = project_path
    error_msg['error_code'] = error_code
    error_msg['error_level'] = error_level
    error_msg['error_memo'] = memo
    error_msg['interface'] = interface
    mqhandler.send_to_queue('log_test', error_msg)


def save_pull_sms_log(param_dict):
    """
    把短讯拉取日志写入消息队列
    :param param_dict:
    :return:
    """
    pull_log = dict()
    pull_log['username'] = param_dict['username']
    pull_log['pullstamp'] = timetool.get_timestamp()
    pull_log['pulldate'] = param_dict['pulldate']
    pull_log['classid'] = param_dict['classid']
    pull_log['product_type'] = param_dict['product_type']
    pull_log['logtype'] = 24
    mqhandler.send_to_queue('log_test', pull_log)


def save_view_log(para_dict):
    """
    保存用户浏览记录
    :param para_dict: 参数dict
    :return:
    """
    ndate = timetool.get_datetime()
    #  因为日志队列还是按老的key来记录数据,所以需要把新key转成老key
    #  等到4.0时viewarticle及消息队列全部换成新key，就不需要进行此转换
    view_article_dict = dict()
    view_article_dict['logtype'] = 4
    view_article_dict['protype'] = para_dict['product_type']
    view_article_dict['requestTime'] = ndate
    view_article_dict['requestDate'] = ndate[0:10]
    view_article_dict['title'] = para_dict['title']
    view_article_dict['token'] = para_dict['token']
    view_article_dict['sccid'] = para_dict['sccid']
    view_article_dict['siteid'] = para_dict['site_id']
    view_article_dict['pname'] = para_dict['class_name']
    view_article_dict['username'] = para_dict['username']
    view_article_dict['userid'] = para_dict['user_id']
    view_article_dict['classid'] = para_dict['class_id']
    view_article_dict['devicetype'] = para_dict['device_type']
    view_article_dict['newsid'] = para_dict['news_id']
    view_article_dict['version'] = para_dict['version']
    view_article_dict['click_source'] = para_dict['click_source']  # 点击来源
    view_article_dict['newskey'] = para_dict['newskey']  # 在获取浏览记录时会用到2016-10-29
    mqhandler.send_to_queue('log_test', view_article_dict)


def save_login_log(param_dict):
    """
    记录登录日志
    :param param_dict:
    :return:
    """
    login_log = dict()
    login_log['username'] = param_dict['user_name']
    login_log['userid'] = param_dict['user_id']
    login_log['token'] = param_dict['device_token']
    login_log['accesstoken'] = param_dict['access_token']
    login_log['ip'] = param_dict['login_ip']
    login_log['siteid'] = 0
    login_log['productversion'] = param_dict['version']
    login_log['producttype'] = param_dict['product_type']
    login_log['devicetype'] = param_dict['device_type']
    login_log['Geo'] = '0,0'
    login_log['signDT'] = timetool.get_current_datetime()
    login_log['logtype'] = 1
    mqhandler.send_to_queue('log_test', login_log)


def save_sign_log(param_dict):
    """
    把签到日志写入消息队列
    :param param_dict:
    :return:
    """
    sign_log = dict()
    sign_log['userid'] = param_dict['user_id']
    sign_log['username'] = param_dict['user_name']
    sign_log['deviceType'] = param_dict['device_type']
    sign_log['geo'] = '0,0'
    sign_log['token'] = param_dict['device_token']
    sign_log['access_token'] = param_dict['access_token']
    sign_log['version'] = param_dict['version']
    sign_log['siteid'] = 0
    sign_log['productType'] = param_dict['product_type']
    sign_log['SignDate'] = timetool.get_current_datetime()
    sign_log['ipAddress'] = param_dict['ip_address']
    sign_log['deviceinfo'] = param_dict['device_info']
    sign_log['logtype'] = 2
    mqhandler.send_to_queue('log_test', sign_log)


def save_logout_log(param_dict):
    """
    把注销日志写入消息队列
    :param param_dict:
    :return:
    """
    logoutlog=dict()
    logoutlog['userid'] = param_dict['user_id']
    logoutlog['devicetype'] = param_dict['device_type']
    logoutlog['version'] = param_dict['version']
    logoutlog['siteid'] = 0
    logoutlog['producttype'] = param_dict['product_type']
    logoutlog['logoutDate'] = timetool.get_current_datetime()
    logoutlog['ipAddress'] = param_dict['remote_ip']
    logoutlog['logtype'] = 3
    mqhandler.send_to_queue('log_test', logoutlog)
